package orest.front.unit;

import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import orest.common.response.Response;
import orest.common.valueobject.UnitVal;
import java.util.*;
import orest.common.dao.UnitDao;
import orest.common.util.*;


public class UnitAction {
	
	/** Creates a new instance of UnitAction */
    public UnitAction() {
    }
   
    public String findUnitByPrk (String uid) {
        Response            rp       = new Response();
        StringBuffer        result   = new StringBuffer();
        UnitDao   obj      = new UnitDao();
        Tools               tool     = new Tools();
        boolean            opReturn = true;
       

       // result.append ("<?xml version='1.0' encoding='gbk'?>");
       // result.append ("<root>");

        /**
         * execute query and get data that were packed in entity bean and
         * saved in arraylist object.
         */
        opReturn = obj.findUnitByPrk(rp, uid);
        if(!opReturn)
            return null;

        /**
         * transfer the object into string.
         */
        ArrayList al = rp.getValObjs ();
        Iterator iterator = al.iterator ();
        Record record = new Record();
        try{
            while(iterator.hasNext ()){
            	
                UnitVal objElem = (UnitVal)iterator.next();
                result.append ("<record>");

                result.append ("<UnitID>");
                result.append (objElem.getUnitID());
                record.addData("unitid", objElem.getUnitID());
                result.append ("</UnitID>");

                result.append ("<Item_9><![CDATA[");
                result.append (objElem.getItem_9());
                record.addData("item_9", objElem.getItem_9());
                result.append ("]]></Item_9>");

                result.append ("<Item_420>");
                result.append (objElem.getItem_420());
                record.addData("item_420", objElem.getItem_420());
                result.append ("</Item_420>");

                result.append ("<Item_37>");
                result.append (objElem.getItem_37());
                record.addData("item_37", objElem.getItem_37());
                result.append ("</Item_37>");

                result.append ("<Item_38>");
                result.append (objElem.getItem_38());
                record.addData("item_38", objElem.getItem_38());
                result.append ("</Item_38>");

                result.append ("<Item_39>");
                result.append (objElem.getItem_39());
                record.addData("item_39", objElem.getItem_39());
                result.append ("</Item_39>");

                result.append ("<Item_40>");
                result.append (objElem.getItem_40());
                record.addData("item_40", objElem.getItem_40());
                result.append ("</Item_40>");

                result.append ("<Item_60>");
                result.append (objElem.getItem_60());
                record.addData("item_60", objElem.getItem_40());
                result.append ("</Item_60>");

                result.append ("<Item_62>");
                result.append (objElem.getItem_62());
                record.addData("item_62", objElem.getItem_62());
                result.append ("</Item_62>");

               

               

                result.append ("<Item_41_3>");
                result.append (objElem.getItem_41_3());
                record.addData("item_41_3", objElem.getItem_41_3());
                result.append ("</Item_41_3>");

                result.append ("<Item_63>");
                result.append (objElem.getItem_63());
                record.addData("item_63", objElem.getItem_63());
                result.append ("</Item_63>");

                result.append ("<Item_64>");
                result.append (objElem.getItem_64());
                record.addData("item_64", objElem.getItem_64());
                result.append ("</Item_64>");

                result.append ("<UnitIntro>");
                result.append ("<![CDATA["+EnCoding.xslEncoder(tool.parseNILL(objElem.getUnitIntro()))+"]]>");
                record.addData("unitintro", objElem.getUnitIntro());
                result.append ("</UnitIntro>");
                              

                result.append ("<NewEngeryClass1>");
                result.append (objElem.getNewEnergyClass1());
                record.addData("newengeryclass1", objElem.getNewEnergyClass1());
                result.append ("</NewEngeryClass1>");

                result.append ("<NewEngeryClass2>");
                result.append (objElem.getNewEnergyClass2());
                record.addData("newengeryclass2", objElem.getNewEnergyClass2());
                result.append ("</NewEngeryClass2>");

                result.append ("<TopicWords>");
                result.append (objElem.getTopicWords());
                record.addData("topicwords",objElem.getTopicWords());
                result.append ("</TopicWords>");

                result.append ("<ChannelShow>");
                result.append (objElem.getChannelShow());
                record.addData("channelshow", objElem.getChannelShow());
                result.append ("</ChannelShow>");

                result.append ("<HomepageShow>");
                result.append (objElem.getHomepageShow());
                record.addData("homepageshow", objElem.getHomepageShow());
                result.append ("</HomepageShow>");

                result.append ("</record>");
                objElem = null;
            }
        }
        catch(Exception e) {
            System.out.println ("when encoding the string, exception occured.");
            System.out.println ("The class is UnitAction and the function is " +
                                 "findUnitByPrk");
        }

      //  result.append ("</root>");

        al = null;
        obj = null;
        rp = null;
        tool = null;

        //return result.toString ();
        return record.getJsonString();
    }
    
  
    
   


   

    public String findUnitByCondition(String key, int pageNo,
                                                      int pageRecords, long ttl) {

        Response            rp       = new Response();
        StringBuffer        result   = new StringBuffer();
        UnitDao        obj      = new UnitDao();
        boolean            opReturn = true;
        String              temp     = "";
        long                total    =0;



        result.append ("<?xml version='1.0' encoding='GBK'?>");
        result.append ("<root>");

        /**
         * execute query and get data that were packed in entity bean and
         * saved in arraylist object.
         */



        try{
            if(pageNo <= 1 || (pageNo > 1 && ttl == 0))
            {
               total = obj.countUnitByCondition(key,rp);
            }
            else
                total = ttl;
            opReturn = obj.findUnitByCondition(rp,key,pageNo, pageRecords,(int)total) ;
            if(!opReturn)
                return null;
        }
        catch(Exception e) {
            e.printStackTrace();
        }


        result.append("<total>");
        result.append(total);
        result.append("</total>");


        ArrayList al = rp.getValObjs ();
        Iterator iterator = al.iterator ();
        RecordSet rset = new RecordSet();
        try{
            while(iterator.hasNext ()){
            	Record record = new Record();
                UnitVal objElem = (UnitVal)iterator.next();

                result.append ("<record>");

                result.append ("<UnitID>");
                result.append (objElem.getUnitID());
                record.addData("unitid",objElem.getUnitID());
                result.append ("</UnitID>");

                result.append ("<Item_9><![CDATA[");
                result.append (objElem.getItem_9());
                record.addData("item_9",objElem.getItem_9());
                result.append ("]]></Item_9>");

                result.append ("<UnitIntro><![CDATA[");
                result.append (objElem.getUnitIntro());
                record.addData("unitintro",objElem.getUnitIntro());
                result.append ("]]></UnitIntro>");
                
                result.append ("<UnitPicture><![CDATA[");
                result.append (objElem.getUnitPicture());
                record.addData("unitpicture",objElem.getUnitPicture());
                result.append ("]]></UnitPicture>");
                
                
                result.append ("</record>");
                
                record.addData("total",total);
                
                
                rset.add(record);
                objElem = null;
            }
        }
        catch(Exception e) {
            System.out.println ("when encoding the string, exception occured.");
            System.out.println ("The class is UnitAction and the function is " +
                                 "findUnitByCondition");
        }

        result.append ("</root>");

        al = null;
        obj = null;
        rp = null;
       

        //return result.toString ();
        return rset.getJsonString();
    }

      public String findTopUnitByCondition (String num,String condition,String sorttype) {
        Response            rp       = new Response();
        StringBuffer        result   = new StringBuffer();
        UnitDao   obj      = new UnitDao();
        Tools               tool     = new Tools();
        boolean            opReturn = true;

      

        /**
         * execute query and get data that were packed in entity bean and
         * saved in arraylist object.
         */
        opReturn = obj.findTopUnitByCondition(rp, num,condition,sorttype);
        if(!opReturn)
            return null;

        /**
         * transfer the object into string.
         */
        ArrayList al = rp.getValObjs ();
        Iterator iterator = al.iterator ();
        try{
            while(iterator.hasNext ()){
                UnitVal objElem = (UnitVal)iterator.next();
                result.append ("<record>");

                result.append ("<UnitID>");
                result.append (objElem.getUnitID());
                result.append ("</UnitID>");

                result.append ("<Item_9><![CDATA[");
                result.append (objElem.getItem_9());
                result.append ("]]></Item_9>");


                result.append ("</record>");
                objElem = null;
            }
        }
        catch(Exception e) {
            System.out.println ("when encoding the string, exception occured.");
            System.out.println ("The class is UnitAction and the function is " +
                                 "findTopUnitByCondition");
        }

        

        al = null;
        obj = null;
        rp = null;
        tool = null;

        return result.toString ();
    }
    
   
}
